package com.ltxtiyu.video.tool;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class NumberTool {

	public static Integer numberNullConvert(String o){
		Integer result = -1;
		try {
			result = Integer.valueOf(o);
		} catch (Exception e) {
			return -1;
		}
		return result;
	}
	
	public static Long numberNullConvert(Object o, Long l){
		Long result = null;
		try {
			result = Long.valueOf(o.toString());
		} catch (Exception e) {
			return l;
		}
		return result;
	}

	public static Integer numberNullConvert(Object o){
		Integer result = -1;
		String str = o+"";
		try {
			result = Integer.valueOf(str);
		} catch (Exception e) {
			return -1;
		}
		return result;
	}
	
	public static BigDecimal numberNullConvert(BigDecimal o){
		return null==o? BigDecimal.ZERO:o;
	}
	

	public static BigDecimal numberNullConvert(String o, BigDecimal b){
		BigDecimal result = BigDecimal.ZERO;
		try {
			result = new BigDecimal(o);
		} catch (Exception e) {
			return b;
		}
		return result;
	}
	public static Integer numberNullConvert(String o, Integer b){
		Integer result = 0;
		try {
			result = Integer.valueOf(o);
		} catch (Exception e) {
			return b;
		}
		return result;
	}
	public static Integer numberNullConvert(Integer o){
		if(null!=o&&o>0)
			return o;
		else
			return -1;
	}
	
	public static Integer numberNullConvert(Integer o, Integer def){
		if(null!=o)
			return o;
		else
			return def;
	}
	
	public static Double numberNullConvert(String o, Double d){
		Double result = 0.0;
		try {
			result = Double.valueOf(o);
		} catch (Exception e) {
			return d;
		}
		return result;
	}
	
	public static String numberFormat(long num1, long num2){
		NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位  
  
        numberFormat.setMaximumFractionDigits(2);  
  
        String result = numberFormat.format((float) num1 / (float) num2 * 100);
  
        return result;
	}

	public static Long floatFormat(String fStr, int rab){
		long a = 0l;
		try {
			return (long)(Float.parseFloat(fStr)*rab);
		}catch (Exception e){
		    return a;
		}
	}
}
